require "gadgets/frame"
require "map_editor/tile_panel"

module MapEditor
  class PaletPanel < TilePanel
    attr_reader :mode, :chipset
    
    def initialize(model, controlers, default_controler)
      super(2)
      @model = model
      
      @controlers = controlers
      @controlers.each do |key, controler|
        controler.target = self
      end
      
      self.controler = @controlers[default_controler]
      
      @scroll_box = Editor::ScrollBox.new(1, 1, self.zoomed_grid_width, self.zoomed_grid_height) do |type|
        self.render
      end

      @chipset = nil
      @active = true

      self.set_panel
      self.set_signals
      self.render
    end
    
    #Property
    def active=(value)
      @active = value
    end
    
    def active?
      return @active
    end
    
    def grid_width
      return @model.grid_width
    end
    
    def grid_height
      return @model.grid_height
    end
        
    def panel_mode=(value)
      return if @panel_mode == value
      @left_pressed = false
      @right_pressed = false
      @panel_mode = value
      self.controler = @controlers[@panel_mode]
      self.render
    end
    
    #Standard Method
    def select(x, y)
      return if self.palet_chips.length <= self.max_x_count * y + x
      return if x >= self.max_x_count || y >= self.max_y_count
    
      @frame.set_size(1, 1)
      @sx = x
      @sy = y
      @ex = @sx
      @ey = @sy
      
      self.render
    end

    def select_chip(palet_chip)
      @frame.set_size(1, 1)
      self.palet_chips.each_with_index do |pc, i|
        if pc == palet_chip
          self.select(i % self.max_x_count, i / self.max_x_count)
          break
        end
      end
      self.render
    end

    def render
      return if @scroll_box.content_image.window.nil?
      return if @scroll_box.width <= 1 || @scroll_box.height <= 1

      if(@dst_texture.nil? || (@dst_texture.width != @scroll_box.width || @dst_texture.height != @scroll_box.height))
        @dst_texture = Texture.new(@scroll_box.width, @scroll_box.height)
      end
      @dst_texture.fill(Color.new(128, 128, 128, 255))

      if @texture == nil || (@texture.width != (@scroll_box.width / self.zoom) || @texture.height != (@scroll_box.height / self.zoom))
        @texture = Texture.new(@scroll_box.width / self.zoom, @scroll_box.height / self.zoom)
      end
      @texture.fill(Color.new(128, 128, 128, 255))

      self.render_samples(@texture)

      self.controler.render(@texture)
      
      @dst_texture.render_texture(@texture, 0, 0, :scale_x => self.zoom, :scale_y => self.zoom)

      self.controler.render_detail(@dst_texture)
      
      buf = Gdk::Pixbuf.new(@dst_texture.dump('rgb'), Gdk::Pixbuf::ColorSpace.new(Gdk::Pixbuf::ColorSpace::RGB), false, 8, @dst_texture.width, @dst_texture.height, @dst_texture.width * 3)
      area = @scroll_box.content_image
      area.window.draw_pixbuf(area.style.fg_gc(area.state), buf, 0, 0, 0, 0, @dst_texture.width, @dst_texture.height, Gdk::RGB::DITHER_NONE, 0, 0)
      p
    end

    def render_samples(s)
      @chipset.render_sample(s, 0, 0, :src_x => self.scroll_x / self.zoom, :src_y => self.scroll_y / self.zoom)
    end

    # Events
    def on_left_down(e)
      self.active = true
      super(e)
    end
        
    def palet_chips
      return @chipset.palet_chips
    end
    
    def get_palet_chip(x, y)
      return self.palet_chips[y * self.max_x_count + x]
    end

    #Iterator
    def each_chip_info
      (0 .. (@sx - @ex).abs).each do |x|
        (0 .. (@sy - @ey).abs).each do |y|
          yield self.get_palet_chip([@sx, @ex].min + x, [@sy, @ey].min + y), x, y
        end
      end
    end

    def on_resize(width, height)
      @scroll_box.set_size_request(width, height)
      p
    end
  end
end